package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.handler.JwtHelper;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author xin Zhou
 * @since 2024-02-02
 */
@RestController
@RequestMapping("/user/userinfo/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @ApiOperation("根据就诊人的Id获取就诊人的信息")
    @GetMapping("/{patientId}")
    public Patient getPatientById(@PathVariable("patientId") Long patientId){
        return patientService.getById(patientId);
    }


    @ApiOperation("增加就诊人")
    @PostMapping("/save")
    public R save(@RequestBody Patient patient, @RequestHeader String token){
//        通过工具类获取到UserId 解析到用户的Id信息
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }


    @ApiOperation("删除就诊人")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        patientService.removeById(id);
        return R.ok();
    }


    @ApiOperation("修改之回显数据")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Long id){
        Patient resultObj = patientService.detail(id);
        return R.ok().data("patient",resultObj);
    }
/*
* 后端注解: SpringMvc之@RquestBody注解
    加了这个注解 前端传送数据使用date 传递数据
    如果不加这个注解,前端传送数据使用Params
* */
    @ApiOperation("修改之更新数据")
    @PutMapping("/update")
    public R update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

//    查询
    @ApiOperation("查询当前用户下的所有信息")
    @GetMapping("/all")
    public R findAll(@RequestHeader String token){
        List<Patient> list = patientService.findAll(token);
        return  R.ok().data("list",list);
    }

}

